package com.hengpeng.itfin.persist.impl;

import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;

import com.hengpeng.common.persist.AbstractDao;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.itfin.persist.BorrowInfoDao;
import com.hengpeng.itfinbase.en.BorrowStatus;
import com.hengpeng.itfinbase.en.InputStatus;
import com.hengpeng.itfinbase.persist.BorrowInfo;

@Repository
@Lazy
public class BorrowInfoDaoImpl extends AbstractDao<BorrowInfo> implements BorrowInfoDao {

    @Override
    public int releaseBorrowMoney(Long borrowInfoId, Long rasieMoney, int raiseNum) {
        return super
                .exeNamedQuery("borrowInfo.releaseBorrowMoney", new Object[] { rasieMoney, raiseNum, borrowInfoId });
    }

    @Override
    public int releaseBorrowMoneyStatus(Long borrowInfoId, Long rasieMoney, int raiseNum, BorrowStatus newStatus) {
        return super.exeNamedQuery("borrowInfo.releaseBorrowMoneyStatus", new Object[] { rasieMoney, raiseNum,
                newStatus, borrowInfoId });
    }

    @Override
    public List<BorrowInfo> queryBorrowList(BorrowStatus status) {
        DynamicQuery query = new DynamicQuery(BorrowInfo.class);
        query.eq("status", status);
        return super.findAllByDynamicQuery(query);
    }

    @Override
    public int borrowStartRaise(BorrowStatus newStatus, Date updateTime, BorrowStatus oldStatus, Date bidOpenTime) {
        return super.exeNamedQuery("borrowInfo.borrowStartRaise", new Object[] { newStatus, updateTime, oldStatus,
                bidOpenTime });
    }

    @Override
    public List<BorrowInfo> getUnAddBidList() {
        DynamicQuery query = new DynamicQuery(BorrowInfo.class);
        query.inArray("status", new BorrowStatus[] { BorrowStatus.SCHEDULED, BorrowStatus.RAISE, BorrowStatus.END });
        query.inArray("inputStatus", new InputStatus[] { InputStatus.WAIT, InputStatus.FAILED });
        return super.findAllByDynamicQuery(query);
    }

    @Override
    public int borrowEndRaise(BorrowStatus newStatus, Date updateTime, BorrowStatus oldStatus, Date bidStopTime) {
        return super.exeNamedQuery("borrowInfo.borrowEndRaise", new Object[] { newStatus, updateTime, oldStatus,
                bidStopTime });
    }

    @Override
    public void updateList(List<BorrowInfo> infoList) {
        super.updateAll(infoList);
    }

    @Override
    public void updateInfo(BorrowInfo borrow) {
        super.update(borrow);
    }

    public BorrowInfo lockInfo(Long id) {
        return super.lockById(id);
    }

    @Override
    public BorrowInfo getBorrowInfo(Long id, BorrowStatus status) {
        DynamicQuery query = new DynamicQuery(BorrowInfo.class);
        query.eq("id", id);
        query.eq("status", status);
        List<BorrowInfo> list = super.findAllByDynamicQuery(query, BorrowInfo.class);
        return list == null || list.size() < 0 ? null : list.get(0);
    }

    @Override
    public int updateRaiseMoney(Long id, Long money) {
        //
        return super.exeNamedQuery("borrowInfo.updateRaiseMoney", new Object[] { money, money, id });
    }

    @Override
    public int updateStatus(Long id, BorrowStatus newStatus, BorrowStatus oldStatus, Date updatTime,
            Date repaymentedTime) {
        return super.exeNamedQuery("borrowInfo.updateStatus", new Object[] { newStatus, updatTime, repaymentedTime, id,
                oldStatus });
    }

    @Override
    public int updateInputStatus(InputStatus newStatus, Date updateTime, Long borrowInfoId, InputStatus oldStatusWait,
            InputStatus oldStatusFailed) {
        return super.exeNamedQuery("borrowInfo.updateInputStatus", new Object[] { newStatus, updateTime, borrowInfoId,
                oldStatusWait, oldStatusFailed });

    }

}
